import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'app/app.dart';
import 'core/providers/global_state_coordinator.dart';
import 'core/providers/hot_item_provider.dart';
import 'core/providers/practice_schedule_provider.dart';
import 'core/providers/discussion_hub_provider.dart';
import 'core/services/data_persistence_manager.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化数据持久化管理器
  final dataPersistenceManager = DataPersistenceManager();
  await dataPersistenceManager.init();

  runApp(MyApp(dataPersistenceManager: dataPersistenceManager));
}

class MyApp extends StatelessWidget {
  final DataPersistenceManager dataPersistenceManager;

  const MyApp({super.key, required this.dataPersistenceManager});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => GlobalStateCoordinator(dataPersistenceManager),
        ),
        ChangeNotifierProvider(create: (context) => HotItemProvider()),
        ChangeNotifierProvider(
          create: (context) => PracticeScheduleProvider(dataPersistenceManager),
        ),
        ChangeNotifierProxyProvider<
          GlobalStateCoordinator,
          DiscussionHubProvider
        >(
          create:
              (context) => DiscussionHubProvider(
                dataPersistenceManager,
                context.read<GlobalStateCoordinator>(),
              ),
          update:
              (context, globalState, previous) =>
                  previous ??
                  DiscussionHubProvider(dataPersistenceManager, globalState),
        ),
      ],
      child: const App(),
    );
  }
}
